/**
 * 
 */
package npap.cater.unr.edu.client.managed.ui.customized;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.shared.EntityProxyId;
import com.google.web.bindery.requestfactory.shared.Receiver;

import npap.cater.unr.edu.client.managed.activity.ApplicationMasterActivities;
import npap.cater.unr.edu.client.managed.activity.ContactListActivity;
import npap.cater.unr.edu.client.managed.activity.FileListActivity;
import npap.cater.unr.edu.client.managed.activity.ProjectActivitiesMapper;
import npap.cater.unr.edu.client.managed.activity.ProjectDetailsActivity;
import npap.cater.unr.edu.client.managed.request.ApplicationEntityTypesProcessor;
import npap.cater.unr.edu.client.managed.request.ApplicationRequestFactory;
import npap.cater.unr.edu.client.managed.ui.ContactListView;
import npap.cater.unr.edu.client.managed.ui.ContactMobileListView;
import npap.cater.unr.edu.client.managed.ui.FileListView;
import npap.cater.unr.edu.client.managed.ui.FileMobileListView;
import npap.cater.unr.edu.client.managed.ui.ProjectDetailsView;
import npap.cater.unr.edu.client.managed.ui.ProjectMobileListView;
import npap.cater.unr.edu.client.proxy.ContactProxy;
import npap.cater.unr.edu.client.proxy.FileProxy;
import npap.cater.unr.edu.client.proxy.ProjectProxy;
import npap.cater.unr.edu.client.scaffold.ScaffoldApp;
import npap.cater.unr.edu.client.scaffold.place.ProxyPlace.Operation;
import npap.cater.unr.edu.client.scaffold.place.customized.AppPlace;
import npap.cater.unr.edu.client.scaffold.place.customized.ProjectListPlace;


/**
 * @author Hao
 * 
 */
public class CustomizedApplicationMasterActivities extends
		ApplicationMasterActivities {
	@Inject
	public CustomizedApplicationMasterActivities(
			ApplicationRequestFactory requests, PlaceController placeController) {
		super(requests, placeController);
		// TODO Auto-generated constructor stub
	}

	@Override
	public Activity getActivity(Place place) {
		// TODO Auto-generated method stub
		if (place instanceof AppPlace) {
			final AppPlace appPlace = (AppPlace) place;
			EntityProxyId<ProjectProxy> proxyID = new EntityProxyId<ProjectProxy>() {

				@Override
				public Class<ProjectProxy> getProxyClass() {
					// TODO Auto-generated method stub
					return ProjectProxy.class;
				}
			};
			return new ApplicationEntityTypesProcessor<Activity>() {

				@Override
				public void handleContact(ContactProxy isNull) {
					if(appPlace.getOperation().equals(Operation.LIST))
	                setResult(new ContactListActivity(requests, ScaffoldApp.isMobile() ? ContactMobileListView.instance() : ContactListView.instance(), placeController));
	            }


				@Override
				public void handleProject(ProjectProxy isNull) {
					// setResult(new ProjectListActivity(requests,
					// ScaffoldApp.isMobile() ? ProjectMobileListView.instance()
					// : ProjectListView.instance(), placeController));
					if(appPlace.getOperation().toString().equals("")||appPlace.getOperation().toString().equals("Details")||appPlace.getOperation()==null)
					{
						setResult(new ProjectDetailsActivity(
								 (EntityProxyId<ProjectProxy>) appPlace.getProxyId(), requests,
								placeController, ProjectDetailsView.instance()));
					}
					else
					{
						setResult(new ProjectActivitiesMapper(requests,
								 placeController).getActivity(appPlace));
					}
					

				}


				@Override
				public void handleFile(FileProxy proxy) {
					// TODO Auto-generated method stub
					if(appPlace.getOperation().equals(Operation.LIST))
		                setResult(new FileListActivity(requests, ScaffoldApp.isMobile() ? FileMobileListView.instance() : FileListView.instance(), placeController));
				}
			}.process(appPlace.getProxyClass());
		}else if (place instanceof ProjectListPlace) {
			final ProjectListPlace projectListPlace = (ProjectListPlace) place;
			
			return new ApplicationEntityTypesProcessor<Activity>() {

				@Override
				public void handleContact(ContactProxy isNull) {
	                setResult(new ContactListActivity(requests, ScaffoldApp.isMobile() ? ContactMobileListView.instance() : ContactListView.instance(), placeController));
	            }

				@Override
				public void handleProject(ProjectProxy isNull) {
				
					requests.projectRequest().findProject(projectListPlace.getIdProject()).fire(new Receiver <ProjectProxy> (){

						@Override
						public void onSuccess(ProjectProxy response) {
							// TODO Auto-generated method stub
							setResult(new ProjectDetailsActivity(
									 (EntityProxyId<ProjectProxy>) response.stableId(), requests,
									placeController, ProjectDetailsView.instance()));
						}
						
					});
					
					

					

				}

				@Override
				public void handleFile(FileProxy proxy) {
					// TODO Auto-generated method stub
					
				}
			}.process(ProjectProxy.class);
		} 
		return super.getActivity(place);

	}

}
